﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class DocumentsSubcategoriesController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Admin/DocumentsSubcategories/
        public ViewResult Index(int CategoryId)
        {
            ViewBag.ParentCategory = db.DocumentsCategories.SingleOrDefault(dc => dc.ID == CategoryId);
            var documentssubcategories = db.DocumentsSubcategories.Include("DocumentsCategories").Where(ds => ds.CategoryId == CategoryId);
            return View(documentssubcategories.ToList());
        }

        // GET: /Admin/DocumentsSubcategories/Details/5
        public ViewResult Details(int id)
        {
            DocumentsSubcategories documentssubcategories = db.DocumentsSubcategories.Single(d => d.ID == id);
            return View(documentssubcategories);
        }

        // GET: /Admin/DocumentsSubcategories/Create
        public ActionResult Create(int CategoryId)
        {
            DocumentsSubcategories model = new DocumentsSubcategories();
            model.CategoryId = CategoryId;
            return View(model);
        } 

        // POST: /Admin/DocumentsSubcategories/Create
        [HttpPost]
        public ActionResult Create(DocumentsSubcategories documentssubcategories)
        {
            if (ModelState.IsValid)
            {
                db.DocumentsSubcategories.AddObject(documentssubcategories);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = documentssubcategories.CategoryId });  
            }

            return View(documentssubcategories);
        }
        
        // GET: /Admin/DocumentsSubcategories/Edit/5
        public ActionResult Edit(int id)
        {
            DocumentsSubcategories documentssubcategories = db.DocumentsSubcategories.Single(d => d.ID == id);
            return View(documentssubcategories);
        }

        //
        // POST: /Admin/DocumentsSubcategories/Edit/5

        [HttpPost]
        public ActionResult Edit(DocumentsSubcategories documentssubcategories)
        {
            if (ModelState.IsValid)
            {
                db.DocumentsSubcategories.Attach(documentssubcategories);
                db.ObjectStateManager.ChangeObjectState(documentssubcategories, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = documentssubcategories.CategoryId }); 
            }
            return View(documentssubcategories);
        }

        //
        // GET: /Admin/DocumentsSubcategories/Delete/5
 
        public ActionResult Delete(int id)
        {
            DocumentsSubcategories documentssubcategories = db.DocumentsSubcategories.Single(d => d.ID == id);
            return View(documentssubcategories);
        }

        //
        // POST: /Admin/DocumentsSubcategories/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            DocumentsSubcategories documentssubcategories = db.DocumentsSubcategories.Single(d => d.ID == id);
            db.DocumentsSubcategories.DeleteObject(documentssubcategories);
            db.SaveChanges();
            return RedirectToAction("Index", new { CategoryId = documentssubcategories.CategoryId }); 
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}